local blaze = require "blaze"
local app = require "app"
local log = blaze.logging.get("room")

local M = {}

M.state = {
    seats = {}, -- <no, Seat>
    players = {} -- <id, GamePlayer>
}

M.mutations = {
    player_enter = function(store, state, player)
        local no = player.seat.no
        if no > 0 then
            state.seats[no] = player.seat
        end
        state.players[player.id] = player
        store.emit("玩家进入", player)
        store.emit("玩家列表变更")
    end,

    player_leave = function(store, state, player)
        state.players[player.id] = nil
        store.emit("玩家离开", player)
        store.emit("玩家列表变更")
    end,

    -- args.player GamePlayer
    -- args.old 原座位号
    set_player_seat = function(store, state, args)
        local old = args.old
        local player = args.player
        if old == 0 then
            store.emit("玩家坐下", args)
            return
        end
        if player.seat.no == 0 then
            store.emit("玩家站起", args)
            return
        end
        store.emit("玩家换座", args)
    end,

    clear = function(store, state, args)
        state.seats = {}
        state.players = {}
        store.emit("玩家列表变更")
    end
}

return M